package com.yyx.qq;

import java.util.ArrayList;

import android.app.TabActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.TabHost;

import com.yyx.qq.helpers.MessageHelper;
import com.yyx.qq.helpers.UIHelper;

import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.QQClient;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.QQUser;
import edu.tsinghua.lumaqq.qq.net.PortGateFactory;

public class QQMain extends TabActivity{

	private static final String TAG = "AndroidQQ";

	private QQClient client;
	private UIHelper uiHelper;
	// Tcp服务器列表
	private String[] TcpServers;
	// Udp服务器列表
	private String[] UdpServers;
	//QQ事件处理
	private QQEventProcessor eventProcessor;
	//QQ消息处理辅助类
	private MessageHelper messageHelper;
	// QQ消息队列
	private MessageQueue mq;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Bundle bundle = getIntent().getExtras();
		int qqnum = bundle.getInt("qqNum");
		String qqpwd = bundle.getString("qqPwd");
		initLayout();
		
		// 初始化登录数据
		initialize();
		doLogin(qqnum, qqpwd);
		
		
	}
	
	/**
	 * 初始化界面布局
	 */
	private void initLayout() {
		TabHost tabHost = getTabHost();// 设置table页
		LayoutInflater.from(this).inflate(R.layout.main_list,
				tabHost.getTabContentView(), true);
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("好友")
				.setContent(R.id.listGroup));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("群").setContent(
				R.id.listCluster));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("最近联系人")
				.setContent(R.id.recentlyConnect));
	}
	
	private void doLogin(int qqnum, String qqpwd) {
		client.setUser(new QQUser(qqnum, qqpwd));
		checkLogin(false, false);
	}

	public UIHelper getUiHelper() {
		return uiHelper;
	}
	public MessageHelper getMessageHelper() {
		return messageHelper;
	}
	public MessageQueue getMq() {
		return mq;
	}
	public QQClient getClient() {
		return client;
	}

	private void initialize() {
		client = new QQClient();
		uiHelper = new UIHelper(this);
		uiHelper.initModels();
		eventProcessor = new QQEventProcessor(this);
		messageHelper = new MessageHelper(this);
		UdpServers = setUdpServers();
		TcpServers = setTcpServers();
	}


	/**
	 * 设置Udp服务器列表，偷懒直接返回数组
	 */
	private String[] setUdpServers() {
		return new String[] { "sz.tencent.com", "sz2.tencent.com",
				"sz3.tencent.com", "sz4.tencent.com", "sz5.tencent.com",
				"sz6.tencent.com", "sz7.tencent.com", "61.144.238.155",
				"61.144.238.156", "202.96.170.163", "202.96.170.164",
				"219.133.38.129", "219.133.38.130", "219.133.38.43",
				"219.133.38.44", "219.133.40.215", "219.133.40.216",
				"219.133.48.100" };
	}

	/**
	 * 设置Tcp服务器列表，偷懒直接返回数组
	 */
	private String[] setTcpServers() {
		return new String[] { "tcpconn.tencent.com", "tcpconn2.tencent.com",
				"tcpconn3.tencent.com", "tcpconn4.tencent.com", "219.133.38.5",
				"218.17.209.23" };
	}

	/**
	 * 检查登陆状态，如果正在登录或者已经登录，直接返回，如果尚未登录则执行登录操作
	 * 
	 * @param forceRandom
	 *            true表示强制随机选择服务器
	 */
	public void checkLogin(boolean forceRandom, boolean forceTcp) {
		if (client.isLogging())
			return;

		client.setConnectionPoolFactory(new PortGateFactory());
		boolean useTcp = forceTcp;
		if (forceRandom) {
			String[] servers = useTcp ? TcpServers : UdpServers;
			client
					.setLoginServer(servers[Util.random().nextInt(
							servers.length)]);
		} else {
			if (useTcp)
				client.setLoginServer(TcpServers[0]);
			else
				client.setLoginServer(UdpServers[0]);
		}
		if (useTcp) {
			client.getUser().setUdp(false);
			client.setTcpLoginPort(80);
		} else
			client.getUser().setUdp(true);
		// 设置参数
		client.addQQListener(eventProcessor);
		try {
			// 登录
			client.login();
		} catch (Exception e) {
			Log.e(TAG,"Login exception", e);
			client.getUser().setStatus(QQ.QQ_STATUS_OFFLINE);
			logout();
		}
	}

	/**
	 * 使自己处于下线状态
	 */
	public void logout() {
		try {
			client.logout();
			client.release();// 释放资源，当你不使用QQ的时候
		} catch (Exception ex) {
			Log.e("error", ex.getMessage());
		}
	}
}
